import { createRouter, createWebHashHistory } from "vue-router";
import { useAccount } from "../store/user";
// let userInfo = null; //模拟用户信息
// 单独提取路由配置，实现路由、菜单的复用
export const routes = [
  {
    path: "/",
    redirect: "/welcome",
    component: () => import("../layout/index.vue"),
    children: [
      {
        path: "/welcome",
        component: () => import("../views/welcome.vue"),
        meta: {
          title: "欢迎页",
          icon: "PieChart",
        },
      },
      {
        path: "/dashboard",
        component: () => import("../views/dashboard/dashboard.vue"),
        meta: {
          title: "数据可视化",
          icon: "PieChart",
        },
      },
      {
        path: "/category",
        component: () => import("../views/category/index.vue"),
        meta: {
          title: "分类管理",
          icon: "Operation",
        },
        children: [
          {
            path: "/category/list",
            component: () => import("../views/category/list.vue"),
            meta: {
              title: "分类列表",
              icon: "Operation",
            },
          },
          {
            path: "/category/pub",
            component: () => import("../views/category/pub.vue"),
            meta: {
              title: "分类发布",
              icon: "Promotion",
            },
          },
        ],
      },
      {
        path: "/company",
        component: () => import("../views/company/index.vue"),
        meta: {
          title: "企业管理",
          icon: "OfficeBuilding",
        },
        children: [
          {
            path: "/company/pub",
            component: () => import("../views/company/pub.vue"),
            meta: {
              title: "企业入驻",
              icon: "OfficeBuilding",
            },
          },
        ],
      },
      {
        path: "/job",
        component: () => import("../views/job/index.vue"),
        meta: {
          title: "岗位管理",
          icon: "HelpFilled",
        },
        children: [
          {
            path: "/job/list",
            component: () => import("../views/job/list.vue"),
            meta: {
              title: "岗位列表",
              icon: "OfficeBuilding",
            },
          },
        ],
      },
      {
        path: "/system",
        component: () => import("../views/system/index.vue"),
        meta: {
          title: "系统管理",
          icon: "Tools",
        },
        children: [
          {
            path: "/system/role",
            component: () => import("../views/system/role.vue"),
            meta: {
              title: "角色管理",
              icon: "OfficeBuilding",
            },
          },
          {
            path: "/system/user",
            component: () => import("../views/system/user.vue"),
            meta: {
              title: "账号管理",
              icon: "OfficeBuilding",
            },
          },
        ],
      },
      {
        path: "/excel",
        component: () => import("../views/excel/index.vue"),
        meta: {
          title: "Excel导入导出",
          icon: "HelpFilled",
        },
        children: [
          {
            path: "/excel/out",
            component: () => import("../views/excel/out.vue"),
            meta: {
              title: "Excel导出",
              icon: "OfficeBuilding",
            },
          },
          {
            path: "/excel/in",
            component: () => import("../views/excel/in.vue"),
            meta: {
              title: "Excel导入",
              icon: "OfficeBuilding",
            },
          },
        ],
      },
    ],
    // beforeEnter: (to, from, next) => {
    //   //当前路由独享守卫
    //   if (userInfo) {
    //     next(); //已登录
    //   } else {
    //     next("/login"); //未登录
    //   }
    // },
  },
  { path: "/login", component: () => import("../views/login.vue") },
  { path: "/nopermit", component: () => import("../views/no-permission.vue") },
  {
    path: "/pinia/test",
    component: () => import("../views/pinia-test/index.vue"),
  },
];
const router = createRouter({
  history: createWebHashHistory(), //设定路由模式
  routes,
});
console.log("路由");

// 路由守卫

let userInfo = { username: "xxx" }; //模拟用户信息
// const user = useAccount() //此处的代码只有在路由首次初始化的时候才会执行
router.beforeEach((to, from, next) => {
  const user = useAccount();
  console.log("路由守卫", to);
  let whiteList = ["/login", "/nopermit", "/welcome"];
  if (whiteList.includes(to.path)) {
    next(); //登录页面
  } else {
    if (user.userInfo) {
      //已登录
      let permit = user.userInfo.permission.includes(to.path);
      if (permit) {
        next(); //有权限
      } else {
        next("/nopermit");
      }
    } else {
      next("/login"); //未登录
    }
  }
});

export default router;
